package webTest.upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IFT8
 * on 2015/4/30.
 */
@WebServlet(name = "Upload", value = "/Upload")
public class Upload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //生成字段List
        ArrayList<String> fieldList = new ArrayList<String>();
        fieldList.add("ids");
        fieldList.add("imgs");
        fieldList.add("urls");
        //根据字段List生成ListMap
        Map<String, ArrayList<String>> fieldListMap = UploadUtils.initListMap(fieldList);
        //处理上传项
        UploadUtils.Upload(request, fieldListMap);
        //处理余下字段信息
        List<String> ids, imgs, urls;
        ids = fieldListMap.get("ids");
        imgs = fieldListMap.get("imgs");
        urls = fieldListMap.get("urls");
        for (int i = 0; i < ids.size(); i++) {
            if (ids.size()>i){
                response.getWriter().print(ids.get(i));
            }
            if (imgs.size()>i){
                response.getWriter().print(imgs.get(i));
            }
            if (urls.size()>i){
                response.getWriter().print(urls.get(i));
            }
            response.getWriter().println();
        }
    }
}
